package com.bayihouse.utils;

import com.baomidou.mybatisplus.extension.service.IService;
import com.bayihouse.model.excel.publicClass.BaseOneEntity;
import com.bayihouse.model.excel.publicClass.father.BaseEntity;
import com.bayihouse.model.excel.publicClass.ExcelResult;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MyObjectUtils {


    public static<T> boolean isArraysEmpty(List<T> objectList) {
        return objectList == null || objectList.size() == 0;
    }


    public static<T> boolean isNull(T object) {
        return object == null;
    }




    public static<T> List<T> dealFlatStreamToList(Stream<List<T>> objectListStream) {
         return objectListStream.flatMap(object->object.stream()).collect(Collectors.toList());
    }



    public static<T> Stream<List<T>> mergeTwoStream(Stream<List<T>> oneStream,Stream<List<T>> otherStream) {
        return Stream.concat(oneStream,otherStream);
    }




    public static<T extends BaseEntity> int getAddIndex(List<T> list, String targetName) throws Exception {
        int left = 0;
        int right = list.size() - 1;
        int res = -1;
        while (left < right) {
            int mid = (left + right) / 2;
            res = MyStringUtils.compareTwo(targetName, list.get(mid).getName());
            if (res == 0) {
                throw new Exception("该对象已存在，无需添加");
            }
            if (res < 0) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public static<T extends BaseEntity> T getObjectByNameToObject(List<T> list, String targetName) {
        int left = 0;
        int right = list.size() - 1;
        int res = -1;
        while (left < right) {
            int mid = (left + right) / 2;
            res = MyStringUtils.compareTwo(targetName, list.get(mid).getName());
            if (res == 0) {
                return list.get(mid);
            }
            if (res < 0) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return null;
    }

    public static<T extends BaseEntity> int getObjectByNameToIndex(List<T> list, String targetName) {
        int left = 0;
        int right = list.size() - 1;
        int res = -1;
        while (left < right) {
            int mid = (left + right) / 2;
            res = MyStringUtils.compareTwo(targetName, list.get(mid).getName());
            if (res == 0) {
                return mid;
            }
            if (res < 0) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }




    public static<T extends BaseEntity> void addObject(List<T> list, T object, int addIndex){
        if (addIndex < 0 || addIndex > list.size()) {
            return;
        }
        list.add(addIndex,object);
    }


    public static<T extends BaseEntity> int addObject(List<T> list, T object) {
        int addIndex = 0;
        try {
            addIndex = getAddIndex(list,object.getName());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return -1;
        }
        if (addIndex < 0 || addIndex > list.size()) {
            return -1;
        }
        list.add(addIndex,object);
        return addIndex;
    }



    public static<T extends BaseEntity,S extends IService<T>> T findObject(List<T> list, T object, S service) {
        int targetIndex = getObjectByNameToIndex(list, object.getName());
        if (MyStringUtils.compareTwo(list.get(targetIndex).getName(),object.getName()) != 0) {
            service.save(object);
            addObject(list,object,targetIndex);
        }
        return list.get(targetIndex);
    }



    public static<T extends BaseEntity> ExcelResult<T> findObject(List<T> list, String targetStr) {
        int targetIndex = getObjectByNameToIndex(list, targetStr);
        if (MyObjectUtils.isArraysEmpty(list)) {
            return new ExcelResult<T>(null,0);
        }
        if (MyStringUtils.compareTwo(list.get(targetIndex).getName(),targetStr)  != 0) {
            return new ExcelResult<T>(null,targetIndex);
        }
        return new ExcelResult<T>(list.get(targetIndex),targetIndex);
    }



}
